
package com.azxc.rapid.library.controller.controller;

import com.azxc.rapid.core.log.annotation.ApiLog;
import com.azxc.rapid.core.redis.lock.RedisLock;
import com.azxc.rapid.core.secure.annotation.PreAuth;
import com.azxc.rapid.core.tool.constant.RoleConstant;
import io.swagger.annotations.Api;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.azxc.rapid.core.boot.ctrl.RapidController;
import com.azxc.rapid.core.mp.support.Condition;
import com.azxc.rapid.core.mp.support.Query;
import com.azxc.rapid.core.tool.api.R;
import com.azxc.rapid.core.tool.utils.Func;

import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.azxc.rapid.library.entity.Book;
import com.azxc.rapid.library.vo.BookVO;
import com.azxc.rapid.library.service.IBookService;


/**
 * 图书表 控制器
 * @since 2023-11-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("book")
@Api(value = "图书管理", tags = "图书管理接口")
public class BookController extends RapidController {

	private final IBookService bookService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入book")
	@ApiLog("图书管理-查询详情")
	@PreAuth("hasAnyRole('administrator', 'bookmanage', 'bookadd', 'bookview')")
	public R<Book> detail(Book book) {
		Book detail = bookService.getOne(Condition.getQueryWrapper(book));
		return R.data(detail);
	}

	/**
	 * 分页 图书表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入book")
	@ApiLog("图书管理-分页查询")
	@PreAuth("hasAnyRole('administrator', 'bookmanage', 'bookadd', 'bookview')")
	public R<IPage<Book>> list(Book book, Query query) {
		IPage<Book> pages = bookService.page(Condition.getPage(query), Condition.getQueryWrapper(book));
		return R.data(pages);
	}

	/**
	 * 自定义分页 图书表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入book")
	@ApiLog("图书管理-自定义分页查询")
	@PreAuth("hasAnyRole('administrator', 'bookmanage', 'bookadd', 'bookview')")
	public R<IPage<BookVO>> page(BookVO book, Query query) {
		IPage<BookVO> pages = bookService.selectBookPage(Condition.getPage(query), book);
		return R.data(pages);
	}

	/**
	 * 新增 图书表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入book")
	@ApiLog("图书管理-新增图书")
	@PreAuth("hasAnyRole('administrator', 'bookmanage', 'bookadd')")
	public R save(@Valid @RequestBody Book book) {
		return R.status(bookService.save(book));
	}

	/**
	 * 修改 图书表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入book")
	@ApiLog("图书管理-修改图书")
	@PreAuth("hasAnyRole('administrator', 'bookmanage', 'bookadd')")
	public R update(@Valid @RequestBody Book book) {
		return R.status(bookService.updateById(book));
	}

	/**
	 * 新增或修改 图书表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入book")
	@ApiLog("图书管理-新增或修改图书")
	@PreAuth("hasAnyRole('administrator', 'bookmanage', 'bookadd')")
	public R submit(@Valid @RequestBody Book book) {
		return R.status(bookService.saveOrUpdate(book));
	}

	
	/**
	 * 删除 图书表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	@ApiLog("图书管理-删除图书")
	@PreAuth("hasAnyRole('administrator', 'bookmanage')")
	//@RedisLock(value = "lock:book:delete")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(bookService.removeByIds(Func.toLongList(ids)));
	}

	
}
